<?php
/**
 * Created by PhpStorm.
 * User: yuanbo
 * Date: 2018/8/12
 * Time: 下午6:55
 */

namespace app\admin\controller;


use think\Controller;

class Activity extends  Controller
{
    //********活动列表***********
    public  function  activityList()
    {
        $activityList = db('selectact')->select();
        $this->assign('activityList',$activityList);
        return $this->fetch('Activity/activityList');
    }
    //**********修改精选活动**********
    public function  updateact()
    {
        //精选活动id
        $selectact_id = input('selectact_id');
        $activity = db('selectact')->where('selectact_id',$selectact_id)->find();
        $this->assign('activity',$activity);
        return $this->fetch('Activity/updateActivity');
    }
    //修改
    public function  update()
    {
        $selectact_id = input('selectact_id');
        $selectact_name = input('selectact_name');
        $selectact_address = input('selectact_address');
        $selectact_time = input('selectact_time');
        $selectact_price = input('selectact_price');
        $selectact_count = input('selectact_count');
        $selectact_phone = input('selectact_phone');
        $selectact_intro = input('selectact_intro');
        $poster = request()->file('poster');
        $data = ['selectact_address'=>$selectact_address,'selectact_time'=>$selectact_time,'selectact_price'=>$selectact_price,
            'selectact_name'=>$selectact_name,'selectact_count'=>$selectact_count,'selectact_phone'=>$selectact_phone,'selectact_intro'=>$selectact_intro];

        if(is_null($poster))
        {

            $rs = db('selectact')->where('selectact_id',$selectact_id)->update($data);
            if ($rs)
            {
                $this->success('修改成功');
            }
            else
            {
                $this->error('修改失败');
            }
        }
        else
        {
            $info = $poster->rule('uniqid')->move('../public/static/img/activity/uploads/poster');
            if ($info) {
                $data['selectact_poster'] =  'uploads/poster/' . $info->getFilename();
            } else {
                //上传失败获取错误信息
                echo $poster->getError();
                $this->error('上传海报失败');
            }
            $rs = db('selectact')->where('selectact_id',$selectact_id)->update($data);
            if ($rs)
            {
                $this->success('修改成功,文件已成功');
            }
            else
            {
                $this->error('修改失败');
            }
        }
    }
    //*********添加精选活动***********
    public function  addActivity()
    {
        return $this->fetch('Activity/addActivity');
    }
    //添加
    public  function  add()
    {
        $selectact_name = input('selectact_name');
        $selectact_address = input('selectact_address');
        $selectact_time = input('selectact_time');
        $selectact_price = input('selectact_price');
        $selectact_count = input('selectact_count');
        $selectact_phone = input('selectact_phone');
        $selectact_intro = input('selectact_intro');
        $poster = request()->file('poster');
        $data = ['selectact_id'=>null,'selectact_address'=>$selectact_address,'selectact_time'=>$selectact_time,'selectact_price'=>$selectact_price,
            'selectact_name'=>$selectact_name,'selectact_count'=>$selectact_count,'selectact_phone'=>$selectact_phone,'selectact_intro'=>$selectact_intro];
        $info = $poster->rule('uniqid')->move('../public/static/img/activity/uploads/poster');
        if ($info) {
            $data['selectact_poster'] =  'uploads/poster/' . $info->getFilename();
            $rs = db('selectact')->insert($data);
            if ($rs)
            {
                $this->success('添加成功，文件已上传');
            }
            else
            {
                $this->error('添加失败');
            }
        } else {
            //上传失败获取错误信息
            echo $poster->getError();
            $this->error('上传海报失败');
        }

    }
    //*************删除精选活动***********
    public function  delete()
    {
        $activity_id = input('activity_id');
        $rs = db('club')->where('selectact_id',$activity_id)->delete();
        $json = [];
        if ($rs)
        {
            $json = ['success'=>1];
        }
        else
        {
            $json = ['success'=>0];
        }
        return json($json);
    }
}